package org.ourgrid.peer.ui.sync.command;

import java.util.List;

import org.ourgrid.common.command.UIMessages;
import org.ourgrid.common.spec.main.DescriptionFileCompile;
import org.ourgrid.common.spec.worker.WorkerSpec;
import org.ourgrid.peer.ui.sync.PeerSyncApplicationClient;
import org.ourgrid.peer.ui.sync.PeerUIMessages;

import br.edu.ufcg.lsd.commune.container.servicemanager.client.sync.command.AbstractCommand;

public class PeerAddAnnotationsCommand extends AbstractCommand<PeerSyncApplicationClient>  {


		public PeerAddAnnotationsCommand(PeerSyncApplicationClient componentClient) {
			super(componentClient);
		}

		protected void execute(String[] params) throws Exception {
			
			if (isComponentStarted()) {
				//TODO LAURO
				List< WorkerSpec > specs;
				if(params[0].endsWith(".sdf")){
					specs = DescriptionFileCompile.compileSDF( params[0] );
				}else{
					specs = DescriptionFileCompile.compileNewSDF( params[0] );
				}
				getComponentClient().addAnnotations( specs );
				System.out.println( PeerUIMessages.getPeerSetWorkersMessage( specs.size() ) );	
			} else {
				printNotStartedMessage();
			}
			
		}

		protected void validateParams(String[] params) throws Exception {
			if ( params == null || params.length != 1 ) {
				throw new IllegalArgumentException( UIMessages.INVALID_PARAMETERS_MSG );
			}
			
		}
		
		private void printNotStartedMessage() {
			System.out.println("Ourgrid Broker is not started.");
		}

}
